package com.dragon.shop.product.service.impl;


import com.dragon.shop.bean.Product;
import com.dragon.shop.bean.User;
import com.dragon.shop.product.mapper.ProductMapper;
import com.dragon.shop.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.LongAdder;

/**
 * @author binghe
 * @version 1.0.0
 * @description 商品业务实现类
 */
@Service
//@RpcService(interfaceClass = ProductService.class, version = "1.0.0", group = "binghe")
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Override
    public Product getProductById(Long pid) {
        return productMapper.selectById(pid);
    }

    @Override
    public int updateProductStockById(Integer count, Long id) {
        return productMapper.updateProductStockById(count, id);
    }

    static class MyData{
        ThreadLocal<Integer> threadLocal = ThreadLocal.withInitial(() -> 0);

        public void add(){
            threadLocal.set(1 + threadLocal.get());
        }
    }

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        MyData myData = new MyData();



        try {
            for(int i=1; i <= 10; i++) {
                executorService.submit(() -> {
                    try {
                        Integer beforeInt = myData.threadLocal.get();
                        myData.add();
                        Integer afterInt = myData.threadLocal.get();
                        System.out.println(Thread.currentThread().getName()+"\t"+"beforeInt:"+beforeInt+"\t afterInt: "+afterInt);
                    } finally {
                        myData.threadLocal.remove();
                    }
                });

                executorService.execute(() -> {
                    System.out.println("a");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }

    }
}
